// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1

import "MenuProcess.js" as MenuProcess
import QtMultimediaKit 1.1

Item{
    id: menu

    property int flagHide: 0
    property bool flagSound: true

    Audio{
        id: audio
        source: "Audio/main.wav"
        volume: 0.5
    }

    Timer{
        id: timerStop
        interval: 40000; running: true; repeat: true
        onTriggered: MenuProcess.restartMusic()
    }
    // start
    Start{
        id: start
        anchors.fill:parent
        onClickedHideStart: {
            MenuProcess.showMenu()
        }
    }

    // options
    Rectangle{
        id: optionsWindow
        anchors.fill: parent
        opacity: 0
        property int flagHideOptions:1

        Image {
            id: backgroundOptions
            source: "Image/background/settingportrait.jpg"
        }

        // button sound
        Image{
            id: buttonSound
            x: 240
            y: parent.height -300
            source: "Image/Button/soundon.png"
            MouseArea{
                anchors.fill: parent
                onPressed: parent.scale = 0.9
                onReleased: parent.scale = 1.0
                onClicked: {
                    if(flagSound == true){
                        buttonSound.source="Image/Button/soundoff.png"
                        flagSound = false
                        audio.stop();
                        timerStop.running = false;
                    }
                    else{
                        buttonSound.source ="Image/Button/soundon.png"
                        flagSound = true
                        audio.play();
                        timerStop.running = true;
                    }
                }
            }

        }
        // button help
        Image{
            id: buttonCallHelp
            x: 57
            y: parent.height -228
            source: "Image/Button/help.png"

            MouseArea{
                anchors.fill: parent
                onPressed: parent.scale = 0.9
                onReleased: parent.scale = 1.0
                onClicked: {
                    optionsWindow.flagHideOptions = 1
                    optionsWindow.state = "optionsHide"
                    helpWindow.flagHideHelp = 0;
                    if(optionsWindow.width < optionsWindow.height)
                        aboutWindow.state = "helpPortrait"
                    else
                        aboutWindow.state = "helpLandscape"
                }

            }

        }
        // button about
        Image{
            id: buttonCallAbout
            x: 245
            y: parent.height -140
            source: "Image/Button/about.png"
            MouseArea{
                anchors.fill: parent
                onPressed: parent.scale = 0.9
                onReleased: parent.scale = 1.0
                onClicked: {
                    optionsWindow.flagHideOptions = 1
                    optionsWindow.state = "optionsHide"
                    aboutWindow.flagHideAbout = 0;
                    if(optionsWindow.width < optionsWindow.height)
                        aboutWindow.state = "aboutPortrait"
                    else
                        aboutWindow.state = "aboutLandscape"
                }

            }

        }
        // button back
        Image{
            id: buttonReturnMFO
            x: 10
            y: parent.height -95
            source: "Image/Button/back.png"
            MouseArea{
                anchors.fill: parent
                onPressed: parent.scale = 0.9
                onReleased: parent.scale = 1.0
                onClicked: {
                    optionsWindow.flagHideOptions = 1
                    optionsWindow.state = "optionsHide"
                    MenuProcess.showMenu()
                }

            }

        }

        states: [
            State {
                name: "optionsHide"
                PropertyChanges {
                    target: optionsWindow
                    opacity: 0
                }
            },
            State{
                name:"optionsLandscape"
                when:((optionsWindow.width > optionsWindow.height)&&(optionsWindow.flagHideOptions == 0))
                PropertyChanges {
                    target: backgroundOptions
                    source: "Image/background/settinglandscape.jpg"
                }
                PropertyChanges {
                    target: optionsWindow
                    opacity: 100
                }
                PropertyChanges {
                    target: buttonSound
                    x: (parent.width - 50)/2
                    y: parent.height -228

                }
                PropertyChanges {
                    target: buttonCallHelp
                    x: 133
                    y: parent.height -180
                }
                PropertyChanges {
                    target: buttonCallAbout
                    x: 530
                    y: parent.height -135
                }
                PropertyChanges {
                    target: buttonReturnMFO
                    x: 10
                    y: parent.height -86
                }

            },
            State{
                name:"optionsPortrait"
                when:((optionsWindow.width < optionsWindow.height)&&(optionsWindow.flagHideOptions == 0))
                PropertyChanges {
                    target: backgroundOptions
                    //source: "Image/background/bgMenu.png"
                }
                PropertyChanges {
                    target: optionsWindow
                    opacity: 100
                }

            }
        ]
    }

    // help
    Rectangle{
        id: helpWindow
        property int flagHideHelp: 1
        anchors.fill: parent
        opacity: 0
        Image {
            id: backgroundHelp
            source: "Image/background/bghelpportrait.jpg"
        }

        Item {
            id: clippingArea

            anchors {
                fill: parent
                topMargin: parent.height * 0.08
                bottomMargin: anchors.topMargin - 15
                leftMargin: parent.width * 0.08
                rightMargin: parent.width * 0.04
            }
            clip: true

            Flickable {
                id: flickable

                anchors {
                    fill: parent
                    rightMargin: 8
                }

                contentWidth: width
                contentHeight: text.height + 2
                flickableDirection: Flickable.VerticalFlick
                clip: true
                Text {
                    id: text

                    width: flickable.width
                    color: "#374b9b"
                    font.family: "Snap ITC"
                    font.pointSize: 9
                    text: "<h2>IQ Love Brains " /*+ container.version + */ + "</h2>" +
                          "<p>IQLoveBrain is a beautiful game for your brain.</p>" +
                          "<p>Controls" +
                          "<ul>Tick on the right answer, then click next</ul>" +
                          "<ul>Each right answer, you will get 10$ " +
                          "If wrong, you will lose 5$</ul></p>" +
                          "<h3>HINTs & TIPS</h3>" +
                          "<p>Time: you have 10 seconds per a question. " +

                          "You have a time saving backet " +
                          "If you answer in a time which are less,  " +
                          "a retain time will be added to time backet " +
                          "If you need more than 10s for a question " +
                          "you can use this time backet for more time " +
                          "if backet has time" +
                          "<p>HINTS: Every question you have 2 hints, but " +
                          "you have to buy it. A hints cost 5$" +
                          "which grid you are looking at.</p>" +
                          "<p>When it is your turn, shoot the enemy ships by " +
                          "tapping one of the grid cells. The view will " +
                          "automatically switch to the enemy grid when it is " +
                          "the enemy's turn. You may also pan the view between " +
                          "your own and the enemy's grid by flicking the grid " +
                          "horizontally. The game ends when all the ships of one " +
                          "of the players have been sunk.</p>" +
                          "<h3>2P game</h3>" +
                          "<p>The 2P game is available in Harmattan and Symbian " +
                          "devices, in Windows build, the 2P Game s are " +
                          "disabled in the menu.</p>" +
                          "<p>To start the 2P game, you must select either Red " +
                          "or Blue grid, the Red one is always the server and " +
                          "Blue the client. The client will begin to search for " +
                          "phones in the vicinity of the phone. " +
                          "All found mobile phones will be listed in the " +
                          "clients view. Select a phone to create a Bluetooth " +
                          "connection between the client and the server.</p>" +
                          "<p>You may now place your ships on the game grid. " +
                          "When you are done, tap the Play button to start the " +
                          "game.</p>"

                    wrapMode: Text.Wrap
                    onLinkActivated: Qt.openUrlExternally(link)
                    opacity: 0.9
                }

            }

            Rectangle {
                id: scrollDecorator

                anchors.right: parent.right

                y: flickable.visibleArea.yPosition * flickable.height +
                   flickable.anchors.topMargin
                width: 4
                height: flickable.visibleArea.heightRatio * flickable.height

                opacity: 0.5
                color: "blue"
                radius: 2

            }
        }


        Image {
            id: backMyButton

            anchors {
                left: parent.left
                bottom: parent.bottom
            }
            source: "Image/Button/back.png"

            MouseArea {
                anchors.fill: parent
                onPressed: parent.scale = 0.9
                onReleased: parent.scale = 1.0
                onClicked: {
                    helpWindow.flagHideHelp = 1
                    helpWindow.state = "helpHide"
                    optionsWindow.flagHideOptions = 0
                    if(aboutWindow.width < aboutWindow.height)
                        optionsWindow.state = "optionsPortrait"
                    else
                        optionsWindow.state = "optionsLandscape"
                }
            }
        }
        states: [
            State {
                name: "helpHide"
                when: buttonReturnMFH.state === "Pressed"
                PropertyChanges {
                    target: helpWindow
                    opacity: 0
                }
            },
            State{
                name:"helpLandscape"
                when:((helpWindow.width > helpWindow.height)&&(helpWindow.flagHideHelp == 0))
                PropertyChanges {
                    target: backgroundHelp
                    source: "Image/background/bghelplandscape.jpg"
                }
                PropertyChanges {
                    target: helpWindow
                    opacity: 100
                }

            },
            State{
                name:"helpPortrait"
                when:((helpWindow.width < helpWindow.height)&&(helpWindow.flagHideHelp == 0))
                PropertyChanges {
                    target: backgroundHelp
                    source: "Image/background/bghelpportrait.jpg"
                }
                PropertyChanges {
                    target: helpWindow
                    opacity: 100
                }

            }
        ]
    }

    // about
    Rectangle{
        id: aboutWindow
        property int flagHideAbout: 1
        anchors.fill: parent
        opacity: 0
        Image {
            id: backgroundAbout
            source: "Image/background/bgaboutportrait.jpg"
        }

        Image {
            id: backMyButtonAbout

            anchors {
                left: parent.left
                bottom: parent.bottom
            }

            sourceSize.width: parent.width * 0.2
            source: "Image/Button/back.png"
            smooth: true

            Behavior on scale {
                PropertyAnimation {
                    duration: 40
                }
            }

            MouseArea {
                anchors.fill: parent
                onPressed: parent.scale = 0.9
                onReleased: parent.scale = 1.0
                onClicked: {
                    aboutWindow.flagHideAbout = 1
                    aboutWindow.state = "aboutHide"
                    optionsWindow.flagHideOptions = 0
                    if(aboutWindow.width < aboutWindow.height)
                        optionsWindow.state = "optionsPortrait"
                    else
                        optionsWindow.state = "optionsLandscape"
                }
            }
        }
        states: [
            State {
                name: "aboutHide"
                PropertyChanges {
                    target: aboutWindow
                    opacity: 0
                }
            },
            State{
                name:"aboutLandscape"
                when:((aboutWindow.width > aboutWindow.height)&&(aboutWindow.flagHideAbout == 0))
                PropertyChanges {
                    target: backgroundAbout
                    source: "Image/background/bgaboutlandscape.jpg"
                }
                PropertyChanges {
                    target: aboutWindow
                    opacity: 100
                }

                PropertyChanges {
                    target: backMyButton
                    sourceSize.width: parent.width * 0.09
                }

            },
            State{
                name:"aboutPortrait"
                when:((aboutWindow.width < aboutWindow.height)&&(aboutWindow.flagHideAbout == 0))
                PropertyChanges {
                    target: backgroundAbout
                    source: "Image/background/bgaboutportrait.jpg"
                }
                PropertyChanges {
                    target: aboutWindow
                    opacity: 100
                }

            }
        ]
    }

    // menu
    Rectangle {
        id: menuWindow
        anchors.fill:parent
        Image {
            id: backgroundMenu
            source: "Image/background/portrait.jpg"
        }

        //buttonStart
        Image{
            id: buttonStart
            x: (parent.width - 360)/2.0
            y: 40
            source: "Image/Button/flag.png"
            MouseArea{
                anchors.fill: parent
                onPressed: parent.scale = 0.9
                onReleased: parent.scale = 1.0
                onClicked: {
                    menu.flagHide = 1;
                    menuWindow.state = "menuHideNoSound"

                    start.flagStartMenuHide = 0;
                    if(menuWindow.width < menuWindow.height){
                        if(menu.flagSound == true){
                            start.flagSound = true
                            start.state ="startMenuWindowFortrait"
                        }
                        else{
                            start.flagSound = false
                            start.state ="startMenuWindowFortraitNoSound"
                        }
                    }
                    else{
                        if(menu.flagSound == true){
                            start.flagSound = true
                            start.state ="startMenuWindowLandscape"
                        }
                        else{
                            start.flagSound = false
                            start.state ="startMenuWindowLandscapeNoSound"
                        }
                    }
                }

            }

        }


        //buttonQuit
        Image{
            id: buttonQuit
            x: (parent.width - 80)
            y: parent.height - 110
            source:"Image/Button/quit.png"
            MouseArea{
                anchors.fill: parent
                onPressed: parent.scale = 0.9
                onReleased: parent.scale = 1.0
                onClicked: {
                    Qt.quit()
                }
            }
        }

        //buttonOptions
        Image{
            id: buttonOptions
            x: 10
            y: parent.height - 110
            source:"Image/Button/options.png"
            MouseArea{
                anchors.fill: parent
                onPressed: parent.scale = 0.9
                onReleased: parent.scale = 1.0
                onClicked: {
                    MenuProcess.hideMenu()
                    if(parent.width < parent.height)
                        optionsWindow.state ="optionsPortrait"
                    else
                        optionsWindow.state ="optionsLanscape"
                    optionsWindow.flagHideOptions = 0;

                }
            }

        }

        //buttonHighScores
        Image{
            id: buttonHighScores
            x: (parent.width - 176)/2.0
            y: parent.height - 280
            //source:"Buttons/highscore1.png"
            MouseArea{
                anchors.fill: parent
                onPressed: parent.scale = 0.9
                onReleased: parent.scale = 1.0
                onClicked: {
                    MenuProcess.hideMenu()
                    if(parent.width < parent.height)
                        highScoreWindow.state ="highScorePortrait"
                    else
                        highScoreWindow.state ="highScoreLanscape"
                    highScoreWindow.flagHideHighScore = 0;

                }
            }
        }

        states: [
            // menuHide
            State {
                name: "menuHide"
                PropertyChanges {
                    target: menuWindow
                    opacity: 0
                }

                PropertyChanges {
                    target: audio
                    playing:true
                }
            },
            // menuHideNoSound
            State {
                name: "menuHideNoSound"
                PropertyChanges {
                    target: menuWindow
                    opacity: 0
                }
                PropertyChanges {
                    target: audio
                    playing: false
                }
                PropertyChanges {
                    target: timerStop
                    running: false
                }
            },
            //menuLandscapeNoSound
            State{
                name:"menuLandscapeNoSound"
                when:((menuWindow.width > menuWindow.height)&&(flagHide == 0)&&(flagSound == false))
                PropertyChanges {
                    target: audio
                    playing: false
                }
                PropertyChanges {
                    target: timerStop
                    running: false
                }
                PropertyChanges {
                    target: backgroundMenu
                   source: "Image/background/landscape.jpg"
                }

                PropertyChanges {
                    target: buttonStart
                    x: (parent.width - 340)/2.0
                    y: parent.height -370

                }
                PropertyChanges {
                    target: buttonOptions
                    x: 10
                    y: parent.height -110

                }

                PropertyChanges {
                    target: buttonQuit
                    x: parent.width - 100
                    y: parent.height -110

                }
                PropertyChanges {
                    target: menuWindow
                    opacity: 100
                }
            },
            //menuLandscape
            State{
                name:"menuLandscape"
                when:((menuWindow.width > menuWindow.height)&&(flagHide == 0)&&(flagSound == true))

                PropertyChanges {
                    target: audio
                    playing:true
                }

                PropertyChanges {
                    target: backgroundMenu
                   source: "Image/background/landscape.jpg"
                }

                PropertyChanges {
                    target: buttonStart
                    x: (parent.width - 340)/2.0
                    y: parent.height -370

                }
                PropertyChanges {
                    target: buttonOptions
                    x: 10
                    y: parent.height -110

                }

                PropertyChanges {
                    target: buttonQuit
                    x: parent.width - 100
                    y: parent.height -110

                }
                PropertyChanges {
                    target: menuWindow
                    opacity: 100
                }
            },
            //menuPortraitNoSound
            State{
                name:"menuPortraitNoSound"
                when:((menuWindow.width < menuWindow.height)&&(flagHide == 0)&&(flagSound == false))
                PropertyChanges {
                    target: audio
                    playing: false
                }
                PropertyChanges {
                    target: timerStop
                    running: false
                }
                PropertyChanges {
                    target: backgroundMenu
                    source: "Image/background/portrait.jpg"
                }
                PropertyChanges {
                    target: menuWindow
                    opacity: 100
                }
            },
            //menuPortrait
            State{
                name:"menuPortrait"
                when:((menuWindow.width < menuWindow.height)&&(flagHide == 0)&&(flagSound == true))

                PropertyChanges {
                    target: backgroundMenu
                    source: "Image/background/portrait.jpg"
                }
                PropertyChanges {
                    target: menuWindow
                    opacity: 100
                }
                PropertyChanges {
                    target: audio
                    playing:true
                }                  
            }
        ]


    }
}
